<?php
	/**
	* Klass ASSIGNMENT HANDLER - Hanterar in och ut data gällande uppdragen ifrån databasen.
	* "Pratar" med databasen och hämtar ut/sätter in information där ifrån och ger tillbaka den informationen till controllern eller view.
	*/
	class AssignmentHandler
	{
		private $database = null;
		private $validate = null;

		// ----- FUNKTION: CONSTRUCT --------------------------------------------------------------------------------------------------------
		public function __construct(Database $database, Validate $validate)
		{
			$this->database = $database;
			$this->validate = $validate;
		}


		// ----- FUNKTION: GET COMPLETED ADMIN ASSIGNMENTS FROM DATABASE --------------------------------------------------------------------
		/**
		 * 
		 * @param
		 * @return 
		 */
		public function GetCompletedAdminAssignmentsFromDatabase()
		{
			$query = "
				SELECT asi.AssignmentID, asi.AssignmentName, asi.StartDate, asi.EndDate, asi.Finished, c.CustomerID, c.Name
				FROM ac AS ac INNER JOIN customer AS c
				ON ac.CustomerID = c.CustomerID
				INNER JOIN assignment AS asi
				ON ac.AssignmentID = asi.AssignmentID
				WHERE asi.Finished = 1
			";
			
			// Förbereder och exekverar SQL-satsen.
			if ($statement = $this->database->PrepareQuery($query)) {
				$statement = $this->database->Select($statement);
				$statement->bind_result($assignmentID, $assignmentName, $assignmentStartDate, $assignmentEndDate, $assignmentFinished, $customerID, $customerName);

				$assignments = array();

				// Läser in alla resultat i en array med assignment-objekt.
				while ($statement->fetch()) {
					$assignment = new Assignment();
					$assignment->assignmentID = $assignmentID;
					$assignment->assignmentCustomerID = $customerID;
					$assignment->assignmentName = $assignmentName;
					$assignment->assignmentStartDate = $assignmentStartDate;
					$assignment->assignmentEndDate = $assignmentEndDate;
					$assignment->assignmentFinished = $assignmentFinished;

					$assignments[] = $assignment;
				}
			}
			return $assignments;
		}


		// ----- FUNKTION: GET ONGOING ADMIN ASSIGNMENTS FROM DATABASE ----------------------------------------------------------------------
		/**
		 * 
		 * @param
		 * @return
		 */
		public function GetOngoingAdminAssignmentsFromDatabase()
		{
			$query = "
				SELECT asi.AssignmentID, asi.AssignmentName, asi.StartDate, asi.EndDate, asi.Finished, c.CustomerID, c.Name
				FROM ac AS ac INNER JOIN customer AS c
				ON ac.CustomerID = c.CustomerID
				INNER JOIN assignment AS asi
				ON ac.AssignmentID = asi.AssignmentID
				WHERE asi.Finished = 0
			";
			
			// Förbereder och exekverar SQL-satsen.
			if ($statement = $this->database->PrepareQuery($query)) {
				$statement = $this->database->Select($statement);
				$statement->bind_result($assignmentID, $assignmentName, $assignmentStartDate, $assignmentEndDate, $assignmentFinished, $customerID, $customerName);

				$assignments = array();

				// Läser in alla resultat i en array med assignment-objekt.
				while ($statement->fetch()) {
					$assignment = new Assignment();
					$assignment->assignmentID = $assignmentID;
					$assignment->assignmentCustomerID = $customerID;
					$assignment->assignmentName = $assignmentName;
					$assignment->assignmentStartDate = $assignmentStartDate;
					$assignment->assignmentEndDate = $assignmentEndDate;
					$assignment->assignmentFinished = $assignmentFinished;

					$assignments[] = $assignment;
				}
			}
			return $assignments;
		}


		// ----- FUNKTION: GET COMPLETED CUSTOMERS ASSIGNMENTS FROM DATABASE ----------------------------------------------------------------
		/**
		 * Funktionen hämtar en specifik kunds alla avslutade uppdrag från databasen.
		 * @param int $customerID Innehåller den specifika kundens ID.
		 * @return array $assignments Retunerar en lista med alla avslutade uppdrag.
		 */
		public function GetCompletedCustomerAssignmentsFromDatabase($customerID)
		{
			$query = "
				SELECT asi.AssignmentID, asi.AssignmentName, asi.StartDate, asi.EndDate, asi.Finished 
				FROM ac AS ac INNER JOIN customer AS c
				ON ac.CustomerID = c.CustomerID
				INNER JOIN assignment AS asi
				ON ac.AssignmentID = asi.AssignmentID
				WHERE ac.CustomerID = ? and asi.Finished = 1
			";
			
			// Förbereder och exekverar SQL-satsen.
			if ($statement = $this->database->PrepareQuery($query)) {
				$statement->bind_param("i", $customerID);
				$statement = $this->database->Select($statement);
				$statement->bind_result($assignmentID, $assignmentName, $assignmentStartDate, $assignmentEndDate, $assignmentFinished);

				$assignments = array();

				// Läser in alla resultat i en array med assignment-objekt.
				while ($statement->fetch()) {
					$assignment = new Assignment();
					$assignment->assignmentID = $assignmentID;
					$assignment->assignmentCustomerID = $customerID;
					$assignment->assignmentName = $assignmentName;
					$assignment->assignmentStartDate = $assignmentStartDate;
					$assignment->assignmentEndDate = $assignmentEndDate;
					$assignment->assignmentFinished = $assignmentFinished;

					$assignments[] = $assignment;
				}
			}
			return $assignments;
		}


		// ----- FUNKTION: GET ONGOING CUSTOMER ASSIGNMENTS FROM DATABASE -------------------------------------------------------------------
		/**
		 * Funktionen hämtar en specifik kunds alla pågående uppdrag från databasen.
		 * @param int $customerID Innehåller den specifika kundens ID.
		 * @return array $assignments Retunerar en lista med alla pågående uppdrag.
		 */
		public function GetOngoingCustomerAssignmentsFromDatabase($customerID)
		{
			$query = "
				SELECT asi.AssignmentID, asi.AssignmentName, asi.StartDate, asi.EndDate, asi.Finished 
				FROM ac AS ac INNER JOIN customer AS c
				ON ac.CustomerID = c.CustomerID
				INNER JOIN assignment AS asi
				ON ac.AssignmentID = asi.AssignmentID
				WHERE ac.CustomerID = ? and asi.Finished = 0
			";
			
			// Förbereder och exekverar SQL-satsen.
			if ($statement = $this->database->PrepareQuery($query)) {
				$statement->bind_param("i", $customerID);
				$statement = $this->database->Select($statement);
				$statement->bind_result($assignmentID, $assignmentName, $assignmentStartDate, $assignmentEndDate, $assignmentFinished);

				$assignments = array();

				//  Läser in alla resultat i en array med assignment-objekt.
				while ($statement->fetch()) {
					$assignment = new Assignment();
					$assignment->assignmentID = $assignmentID;
					$assignment->assignmentCustomerID = $customerID;
					$assignment->assignmentName = $assignmentName;
					$assignment->assignmentStartDate = $assignmentStartDate;
					$assignment->assignmentEndDate = $assignmentEndDate;
					$assignment->assignmentFinished = $assignmentFinished;

					$assignments[] = $assignment;
				}
			}
			return $assignments;
		}


		// ----- FUNKTION: ADD NEW ASSIGNMENT -----------------------------------------------------------------------------------------------
		/**
		 * Funktion som lägger till ett nytt uppdrag på en specifik kund.
		 * @param string $assignment Innehåller det specifika uppdraget.
		 * @return string $newAssignmentID Retunerar det ny uppdragets ID.
		 * @return bool true/false Retunerar false om det inte gick att förbereda SQL-dsatsen för exekvering.
		 */
		public function AddNewAssignment($assignment)
		{
			$query = "INSERT INTO assignment (AssignmentName, StartDate, EndDate) VALUES(?, ?, ?)";
			
			// Sätter in det nya uppdraget med tillhörande information i databasen.
			if ($insertStatement = $this->database->PrepareQuery($query)) {

				$assignmentName = $assignment->assignmentName;
				$assignmentStartDate = $assignment->assignmentStartDate;
				$assignmentEndDate = $assignment->assignmentEndDate;

				$insertStatement->bind_param("sss", $assignmentName, $assignmentStartDate, $assignmentEndDate);
				$newAssignmentID = $this->database->Insert($insertStatement);

				return $newAssignmentID;
			}
			return false;
		}


		// ----- FUNKTION: ADD NEW ASSIGNMENT CUSTOMER --------------------------------------------------------------------------------------
		/**
		 * Funktion som lägger till en ny kund på ett specifikt uppdrag.
		 * @param int $customerID Innehåller den specifika kundens ID.
		 * @param int $assignmentID Innehåller det specifika uppdragets ID.
		 * @return bool true/false Retunerar false om det inte gick att förbereda SQL-dsatsen för exekvering.
		 */
		public function AddNewAssignmentCustomer($customerID, $assignmentID)
		{
			$query = "INSERT INTO ac (CustomerID, AssignmentID) VALUES(?, ?)";
			
			// Sätter in customerID och assignmentID i databasen.
			if ($insertStatement = $this->database->PrepareQuery($query)) {
				$insertStatement->bind_param("ii", $customerID, $assignmentID);
				$newACID = $this->database->Insert($insertStatement);
				return $insertStatement;
			}
			return false;
		}


		// ----- FUNKTION: MARK ASSIGNMENT AS FINISHED --------------------------------------------------------------------------------------
		/**
		 * Funktion som markerar ett uppdrag som avslutat
		 * @param string $assignmentID ID till det uppdrag som ska avslutas
		 * @return int Det avslutade uppdragets ID
		 */
		public function MarkAssignmentAsFinished($assignmentID)
		{
			$query = "UPDATE assignment SET Finished = 1 WHERE AssignmentID = ?";

			if ($updateStatement = $this->database->PrepareQuery($query)) {
				$updateStatement->bind_param("i", $assignmentID);
				$assignmentID = $this->database->Update($updateStatement);

				return $assignmentID;
			}
			return false;
		}


		// ----- FUNKTION: DATA VALIDATOR ---------------------------------------------------------------------------------------------------
		/**
		 * Funktion som validerar indatan från uppdrags formuläret.
		 * @param string $assignmentName Innehåller uppdragsnamnet som ska valideras.
		 * @param string $startDate Innehåller startdatumet som ska valideras.
		 * @param string $endDate Innehåller slutdatumet som ska valideras.
		 * @return string $returnstring
		 */
		public function DataValidator($assignmentName, $startDate, $endDate)
		{
			$messagesNumber = array();

			// Kontrollerar om valideringen i VALIDATE gick igenom utan fel. Fick vi några fel går vi in i nästa if-sats.
			if($this->validate->ValidateAssignmentname($assignmentName) == false){
				
				// Kontrollerar att strängen inte är tom eller har fel format.
				if (strlen($assignmentName) == 0 || $assignmentName == ""){
					$messagesNumber[] = AssignmentView::EMPTY_ASSIGNMENTNAME;	
				} else {
					$messagesNumber[] = AssignmentView::WRONG_FORMAT_ASSIGNMENTNAME;
				}
			}

			// Kontrollerar om valideringen i VALIDATE gick igenom utan fel. Fick vi några fel går vi in i nästa if-sats.
			if($this->validate->ValidateStartdate($startDate) == false){
				// Kontrollerar att strängen inte är tom eller har fel format.
				if (strlen($startDate) == 0 || $startDate == ""){
					$messagesNumber[] = AssignmentView::EMPTY_STARTDATE;	
				} else {
					$messagesNumber[] = AssignmentView::WRONG_FORMAT_STARTDATE;
				}
			}

			// Kontrollerar om valideringen i VALIDATE gick igenom utan fel. Fick vi några fel går vi in i nästa if-sats.
			if($this->validate->ValidateEnddate($endDate) == false){
				// Kontrollerar att strängen inte är tom eller har fel format.				
				if (strlen($endDate) == 0 || $endDate == ""){
					$messagesNumber[] = AssignmentView::EMPTY_ENDDATE;	
				} else {
					$messagesNumber[] = AssignmentView::WRONG_FORMAT_ENDDATE;
				}
			}
			return $messagesNumber;			
		}
	}
?>